package servlet;

import dao.impI.UserDaoImp;
import service.ImpI.UserServiceImpI;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns = "/register",loadOnStartup = 1,
        initParams = {
                @WebInitParam(name = "encoding",value = "utf-8")
        }
)
public class RegisterServlet extends HttpServlet {

    String encoding=null;

    @Override
    public void init(ServletConfig config) throws ServletException {
        encoding = config.getInitParameter("encoding");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding(encoding);
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println(username+password);
        //UserDaoImp userDaoImp = new UserDaoImp();
        //boolean flag = userDaoImp.register(username, password);
        UserServiceImpI userServiceImpI = new UserServiceImpI();
        boolean flag = userServiceImpI.register(username, password);
        System.out.println(flag);
        if (flag){
            req.getRequestDispatcher("/login1.jsp").forward(req,resp);
        }else{
            req.getRequestDispatcher("/exception.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);{

        }
    }
}
